define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    $("#authlogin").click(function (){
        var checkedbox= $("#table").bootstrapTable('getSelections'); 	//获取选中的数据
        var leng = checkedbox.length;
        if(leng == 1){
            var idsArr = [];
            for(var i=0;i<leng;i++){
                var oneItem = checkedbox[i];
                idsArr.push(oneItem.id);
            }
            var index_load = layer.load();
            var ids = idsArr.join(',');
            var url = "dingdan/phone/authlogin?ids="+ids;
            $.get(url,function (res){
                layer.close(index_load);
                if(res.code == 1){
                    window.open(res.data.loginurl,'_blank');
                    // top.location.href = res.data.loginurl;
                }else{
                    Layer.msg(res.msg);
                }
            });
        }else if(leng > 1){
            Layer.msg("请选择一个进行操作");
        }else{
            Layer.msg("请选择记录");
        }
    });
    $("#reboot").click(function (){
        var checkedbox= $("#table").bootstrapTable('getSelections'); 	//获取选中的数据
        var leng = checkedbox.length;
        if(leng == 1){
            var idsArr = [];
            for(var i=0;i<leng;i++){
                var oneItem = checkedbox[i];
                idsArr.push(oneItem.id);
            }

            var ids = idsArr.join(',');
            var url = "dingdan/phone/reboot?ids="+ids;

            Layer.confirm("确定重启么", {
                title: "提示",
                icon: 0,
                btn: ['确认'],
                btnAlign: "r",
            }, function(index, layero){
                var index_load = layer.load();
                $.get(url,function (res){
                    Layer.close(index_load);
                    Layer.msg(res.msg);
                });
            });

        }else if(leng > 1){
            Layer.msg("请选择一个进行操作");
        }else{
            Layer.msg("请选择记录");
        }
    });
    var Controller = {
        index: function () {
            var window_innerheight = window.innerHeight;
            var table_height = window_innerheight-20;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dingdan/phone/index',
                    add_url: 'dingdan/phone/add',
                    edit_url: 'dingdan/phone/edit',
                    del_url: 'dingdan/phone/del',
                    multi_url: '',
                    table: 'phone',
                },

                pageSize: Config.pagesize,
                height: table_height
            });

            var table = $("#table");
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#','');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.is_delete = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'instance_code', title: "实例编号"},
                        {field: 'name', title: "名字", operate: false},


                        {field: 'package.name', title: "套餐类型",operate: false},
                        {field: 'package_id', title: "选择套餐",visible: false,addClass: "selectpage", extend: "data-source='dingdan/phone/ajaxPackage' data-field='name' data-page-size='20'",placeholder:"请选择套餐"},


                        {field: 'status', title: "状态",formatter: Controller.api.formatter.setStatus, searchList: {'1':"上架", '-1':"下架"}},
                        {field: 'is_use_allday', title: "整天占用",formatter: Controller.api.formatter.setAllday, searchList: {'1':"是", '-1':"否"}},

                        {field: 'init_status', title: "是否就绪",formatter: Controller.api.formatter.setJiuxu,searchList: {'-1':"执行失败", '1':"就绪",'2':"还原中"}},
                        {field: 'is_use', title: "空闲状态",formatter: Controller.api.formatter.setUse,operate: false},

                        // {field: 'update_time', title: "修改时间", operate: false},
                        {field: 'create_time', title: '创建时间',addclass:'datetimerange w200', operate:'RANGE', formatter: Table.api.formatter.datetime},


                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 打开方式
            Controller.api.bindevent();
        },
        edit: function () {
            // 打开方式
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                setStatus: function (value, row, index) {
                    if(row.status == 1){
                        var html = '<span class="label label-success">上架</span>';
                    }else{
                        var html = '<span class="label label-danger">下架</span>';
                    }
                    return html;
                },
                setUse: function (value, row, index) {
                    if(row.is_use == 1){
                        var html = '<span class="label label-primary">租用中</span>';
                    }else{
                        var html = '<span class="label label-default">闲置</span>';
                    }
                    return html;
                },
                setJiuxu: function (value, row, index) {
                    if(row.init_status == 1){
                        var html = '<span class="label label-success">就绪</span>';
                    }else if(row.init_status == 2){
                        var html = '<span class="label label-warning">还原中</span>';
                    }else{
                        var html = '<span class="label label-danger">执行失败</span>';
                    }
                    return html;
                },
                setType: function (value, row, index) {
                    if(row.type == 1){
                        var html = '<span class="label label-success">高级配置</span>';
                    }else if(row.type == 2){
                        var html = '<span class="label label-info">豪华配置</span>';
                    }
                    return html;
                },
                setAllday: function (value, row, index) {
                    if(row.is_use_allday == 1){
                        var html = '<span class="label label-success">是</span>';
                    }else{
                        var html = '<span class="label label-warning">否</span>';
                    }
                    return html;
                },
            }
        }
    };
    return Controller;
});
